<?php

/**
 * 微信投票奖品 Controller
 * 
 * @path Apps/Mp/Controller/WxEventVoteAwardController.class.php
 * @author Zix <zix2002@gmail.com> 
 * @version v1.0 , 2015-09-11
 */

namespace Mp\Controller;

use Common\Service\WxEventVoteAwardService;

class WxEventVoteAwardController extends MpController {

  public function index( $event_id ) {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '投票奖品';
    $data['module_url'] = U($this->base_path . 'index');

    $WxEventVoteAward = WxEventVoteAwardService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => 'menu_Mp_MerWxSetting_index',
      'event_id' => $event_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url' => U($this->base_path . 'read') . "/$event_id",
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'upload_icon_url' => U($this->base_path . 'upload') .'/icon',
      'del_icon_url' => U($this->base_path . 'delete_icon') ,
      'award_sn_url' => U('Mp/WxEventVoteAwardSn/index') . '/',
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $WxEventVoteAward->get_default_row()
    );

    $data['param'] = array(
      'rank' => $WxEventVoteAward->rank ,
      'type' => $WxEventVoteAward->type ,
      'status' => $WxEventVoteAward->status ,
    );

    $this->display_with_layout($data, $this->controller . ':index');
  }

  function read($event_id) {
    $post = I('post.');

    $config = array(
      'event_id' => $event_id ,
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $WxEventVoteAward = WxEventVoteAwardService::instance();
    $data['rows'] = $WxEventVoteAward->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $WxEventVoteAward->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    
    $WxEventVoteAward = WxEventVoteAwardService::instance();
    $ret = $WxEventVoteAward->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $WxEventVoteAward = WxEventVoteAwardService::instance();
    $ret = $WxEventVoteAward->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $WxEventVoteAward = WxEventVoteAwardService::instance();
    $ret = $WxEventVoteAward->delete($ids);
    $this->ajaxReturn($ret);
  }

}
